/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "stdafx.h"
#include "光追后端.h"
#include "底层绘图/底层绘图框架.h"
#include "纹理/纹理.h"
#include "shader_std.h"

#include <file_文件名数据.h>





C_Vk光追后端::C_Vk光追后端(S_设备环境& ctx) {
	m_Ctx = &ctx;


	S_Material 默认材质;
	默认材质.m_shaderIndex = -1;

	默认材质.m_color = { 1,0,0.8 };
	默认材质.m_colorTex = -1;

	默认材质.m_emission = {};			//发光
	默认材质.m_emissionTex = -1;

	默认材质.m_reflect = 0;					//反射度
	默认材质.m_reflectTex = -1;

	默认材质.m_refract = 0;					//折射
	默认材质.m_refractTex = -1;

	默认材质.m_roughness = 0.5;				//粗糙度
	默认材质.m_roughnessTex = -1;

	默认材质.m_alpha = 1;
	默认材质.m_alphaTex = -1;

	默认材质.m_normalTex = -1;
	默认材质.m_shaderIndex = -1;

	默认材质.m_transmission = 0;


	

	*f_buf_Material_ptr(f_buf_getGlobal().m_材质) = 默认材质;

	if (ctx.m_支持光追 == false) {
		return;
	}


	m_光追管线属性 = {};
	m_光追管线属性.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_RAY_TRACING_PIPELINE_PROPERTIES_KHR;

	VkPhysicalDeviceProperties2 deviceProperties2{};
	deviceProperties2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
	deviceProperties2.pNext = &m_光追管线属性;

	vkGetPhysicalDeviceProperties2((VkPhysicalDevice)(m_Ctx->m_物理设备.设备), &deviceProperties2);


	//S_绘图设备配置* 绘图环境 = (S_绘图设备配置*)f_draw_创建绘图设备(ctx);
	auto* 绘图环境 = f_vk_get绘图环境(ctx);
	//m_绘图环境 = (S_结构指针)绘图环境;

	
	g_加速结构创建命令缓存 = (S_结构指针)f_vk创建命令缓存(绘图环境, 1, true);
	m_渲染命令缓存 = (S_结构指针)f_vk创建命令缓存(绘图环境, 1, true);
	g_渲染Fence = (S_结构指针)f_vk创建Fence(绘图环境);
	//f_创建渲染环境()
	

	m_渲染纹理 = f_tex_创建存储2D纹理(ctx, { 1024,1024,1 }, E_纹理格式::e_tf_RGBA8U_归一化, E_纹理用途::e_贮存 | E_纹理用途::e_转移源);
	
	//m_渲染纹理 = f_tex_创建存储2D纹理(ctx, { 1024,1024,1 }, E_纹理格式::e_tf_RGBA32, E_纹理用途::e_贮存 | E_纹理用途::e_转移源);
	//m_底层加速结构缓存 = f_buf_create板载缓存(*m_Ctx, 1, E_板载缓存类型::e_光追底层加速结构, 1);
	//m_顶层加速结构 = f_buf_create板载缓存(*m_Ctx, 1, E_板载缓存类型::e_光追顶层加速结构, 1);
	m_物体指针 = f_bm_alloc(f_buf_getGlobal().g_UI64缓存, 1);
	m_RtOb = f_bm_alloc(f_buf_getGlobal().g_光追物体实例属性ID, 1);


	m_顶层实例加速结构 = f_buf_create板载缓存(*m_Ctx, sizeof(VkAccelerationStructureInstanceKHR), E_板载缓存类型::e_光追实例, 1);
	m_底层暂存缓冲区 = f_buf_create板载缓存(*m_Ctx, E_板载缓存类型::e_SSBO, E_板载缓存类型::e_SSBO | E_板载缓存类型::e_着色端内存地址, 1, 1);
	m_顶层暂存缓冲区 = f_buf_create板载缓存(*m_Ctx, E_板载缓存类型::e_SSBO, E_板载缓存类型::e_SSBO | E_板载缓存类型::e_着色端内存地址, 1, 1);
	m_顶层暂存缓冲区_1 = f_buf_create板载缓存(*m_Ctx, E_板载缓存类型::e_SSBO, E_板载缓存类型::e_SSBO | E_板载缓存类型::e_着色端内存地址, 1, 1);
	
	
	m_投影矩阵 = f_buf_create板载缓存(ctx, sizeof(Mat44f), E_板载缓存类型::e_SSBO);


	//m_顶层加速结构->m_预分配空间 = false;
	//m_底层暂存缓冲区->m_预分配空间 = false;
	//
	//m_顶层暂存缓冲区->m_预分配空间 = false;
	//m_顶层暂存缓冲区_1->m_预分配空间 = false;


	m_生成着色列表 = f_buf_create板载缓存(*m_Ctx, 1, E_板载缓存类型::e_着色列表, 1);
	m_忽略着色列表 = f_buf_create板载缓存(*m_Ctx, 1, E_板载缓存类型::e_着色列表, 1);
	m_相交着色列表 = f_buf_create板载缓存(*m_Ctx, 1, E_板载缓存类型::e_着色列表, 1);
	//m_AABB着色列表 = f_buf_create板载缓存(*m_Ctx, 1, E_板载缓存类型::e_着色列表, 1);
	m_回调着色列表 = f_buf_create板载缓存(*m_Ctx, 1, E_板载缓存类型::e_着色列表, 1);

	{
		m_帧渲染信息 = f_buf_create板载缓存(*m_Ctx, sizeof(GLSL_RenderFrame), E_板载缓存类型::e_UBO, 2);
		m_mapPtr_渲染帧数据 = f_buf_map板载缓存<GLSL_RenderFrame>(m_帧渲染信息);
		m_mapPtr_渲染帧数据->m_Frame = 0;
		m_mapPtr_渲染帧数据->m_ParallelLightNum = 0;
		m_mapPtr_渲染帧数据->m_SpotLightNum = 0;
		m_mapPtr_渲染帧数据->m_PointLightNum = 0;
		m_mapPtr_渲染帧数据->m_AreaLightNum = 0;
		m_mapPtr_渲染帧数据->m_BouncesNum = 4;
		m_mapPtr_渲染帧数据->m_LigthSamples = 4;
		m_mapPtr_渲染帧数据->m_SamplesNum = 4;
		m_mapPtr_渲染帧数据->m_world_ShaderID = -1;

		m_mapPtr_渲染帧数据->m_ViewOffset_X = 0;
		m_mapPtr_渲染帧数据->m_ViewOffset_Y = 0;
		m_mapPtr_渲染帧数据->m_MaxZ = 5000;
		m_mapPtr_渲染帧数据->m_OutLayer_PreviewMode = DEF_RF_PM_composite | 1;
	}
	

	{
		m_渲染纹理采样器 = f_tex_创建纹理采样器(ctx);
		

		m_渲染纹理_最终合成 = f_tex_创建图像纹理(ctx, { 16, 16, 1 }, E_纹理格式::e_tf_RGBA32, E_纹理维度类型::e_2D);
		f_tex_安装纹理采样器(m_渲染纹理_最终合成, m_渲染纹理采样器);

		m_渲染纹理_漫反射 = f_tex_创建图像纹理(ctx, { 16, 16, 1 }, E_纹理格式::e_tf_RGBA32, E_纹理维度类型::e_2D);
		f_tex_安装纹理采样器(m_渲染纹理_漫反射, m_渲染纹理采样器);

		//m_渲染纹理_深度 = f_tex_创建深度纹理(ctx, { 16, 16, 1 }, E_MS次数::e_MS_1);
		m_渲染纹理_深度 = f_tex_创建图像纹理(ctx, { 16, 16, 1 }, E_纹理格式::e_tf_R32, E_纹理维度类型::e_2D);
		f_tex_安装纹理采样器(m_渲染纹理_深度, m_渲染纹理采样器);

		m_渲染纹理_法线 = f_tex_创建图像纹理(ctx, { 16, 16, 1 }, E_纹理格式::e_tf_RGBA32, E_纹理维度类型::e_2D);
		f_tex_安装纹理采样器(m_渲染纹理_法线, m_渲染纹理采样器);

		m_渲染纹理_自发光 = f_tex_创建图像纹理(ctx, { 16, 16, 1 }, E_纹理格式::e_tf_RGBA32, E_纹理维度类型::e_2D);
		f_tex_安装纹理采样器(m_渲染纹理_自发光, m_渲染纹理采样器);

		m_渲染纹理_光照 = f_tex_创建图像纹理(ctx, { 16, 16, 1 }, E_纹理格式::e_tf_R32, E_纹理维度类型::e_2D);
		f_tex_安装纹理采样器(m_渲染纹理_光照, m_渲染纹理采样器);

		m_渲染纹理_最终合成->m_TexID = E_RenderLayerType::e_renderLayer_composite;
		m_渲染纹理_漫反射->m_TexID = E_RenderLayerType::e_renderLayer_diffuse;
		m_渲染纹理_深度->m_TexID = E_RenderLayerType::e_renderLayer_depth;
		m_渲染纹理_法线->m_TexID = E_RenderLayerType::e_renderLayer_normal;
		m_渲染纹理_自发光->m_TexID = E_RenderLayerType::e_renderLayer_illuminate;
		m_渲染纹理_光照->m_TexID = E_RenderLayerType::e_renderLayer_shadow;

		std::vector<vec4> 默认纹理填充(16 * 16);
		f_tex_填充纹理数据(m_渲染纹理_最终合成, (uint8*)默认纹理填充.data(), sizeof(vec4), { 16,16,1 });
		f_tex_填充纹理数据(m_渲染纹理_漫反射, (uint8*)默认纹理填充.data(), sizeof(vec4), { 16,16,1 });
		f_tex_填充纹理数据(m_渲染纹理_深度, (uint8*)默认纹理填充.data(), sizeof(float32), { 16,16,1 });
		f_tex_填充纹理数据(m_渲染纹理_法线, (uint8*)默认纹理填充.data(), sizeof(vec4), { 16,16,1 });
		f_tex_填充纹理数据(m_渲染纹理_自发光, (uint8*)默认纹理填充.data(), sizeof(vec4), { 16,16,1 });
		f_tex_填充纹理数据(m_渲染纹理_光照, (uint8*)默认纹理填充.data(), sizeof(float32), { 16,16,1 });

		f_add纹理(m_渲染纹理_最终合成, E_RenderLayerType::e_renderLayer_composite, E_纹理分组类型::e_渲染输出层);
		f_add纹理(m_渲染纹理_漫反射, E_RenderLayerType::e_renderLayer_diffuse, E_纹理分组类型::e_渲染输出层);
		f_add纹理(m_渲染纹理_深度, E_RenderLayerType::e_renderLayer_depth, E_纹理分组类型::e_渲染输出层);
		f_add纹理(m_渲染纹理_法线, E_RenderLayerType::e_renderLayer_normal, E_纹理分组类型::e_渲染输出层);
		f_add纹理(m_渲染纹理_自发光, E_RenderLayerType::e_renderLayer_illuminate, E_纹理分组类型::e_渲染输出层);
		f_add纹理(m_渲染纹理_光照, E_RenderLayerType::e_renderLayer_shadow, E_纹理分组类型::e_渲染输出层);
	}

	m_底层加速结构内存分配大小 = 0;
}

C_Vk光追后端::~C_Vk光追后端() {
	//auto* 绘图配置 = (S_VK绘图设备*)m_绘图环境;
	auto* 绘图配置 = f_vk_get绘图环境(*m_Ctx);
	//auto* 绘图框架 = f_vk_get绘图框架(*m_Ctx);

	if(m_Ctx->m_支持光追 == false) return;

	f_buf_unmap板载缓存(m_帧渲染信息);
	

	f_buf_release板载缓存(m_顶层暂存缓冲区);
	f_buf_release板载缓存(m_顶层暂存缓冲区_1);
	f_buf_release板载缓存(m_底层暂存缓冲区);
	f_buf_release板载缓存(m_顶层加速结构);
	//f_buf_release板载缓存(m_底层加速结构缓存);
	f_buf_release板载缓存(m_生成着色列表);
	f_buf_release板载缓存(m_忽略着色列表);
	//f_buf_release板载缓存(m_AABB着色列表);
	f_buf_release板载缓存(m_相交着色列表);
	f_buf_release板载缓存(m_回调着色列表);

	//for(auto& e : m_底层加速结构缓存组) f_buf_release板载缓存(e.second);
	for(auto& e : m_顶层加速结构缓存组) f_buf_release板载缓存(e.second);


	f_buf_release板载缓存(m_帧渲染信息);

	f_tex_销毁纹理(m_渲染纹理);


	for (auto& e : ((S_Vk光追线管*)m_光追管线)->m_着色->m_着色阶段创建信息) {
		vkDestroyShaderModule(((S_Vk光追线管*)m_光追管线)->m_着色->m_设备, e.module, 0);
	}
	vkDestroyPipeline(绘图配置->m_设备, ((S_Vk光追线管*)m_光追管线)->m_线管, nullptr);


	f_vk销毁Fence(绘图配置, g_渲染Fence);
	f_vk销毁命令缓存(绘图配置, (VkCommandBuffer)m_渲染命令缓存);

	
	f_draw_销毁绘图设备(绘图配置);
}

uint32 offset = 0;
void C_Vk光追后端::f_重置() {
	for (auto& e : m_底层加速结构绑定信息) free(e);

	//m_几何加速结构.clear();
	m_底层加速结构绑定信息.clear();
	m_几何图元数量.clear();

	m_底层加速结构内存分配大小 = 0;
	//m_底层加速结构缓存组.resize()
}

void C_Vk光追后端::f_添加底层加速结构(S_Mesh* me, uint32 id, E_物体类型 ObType, uint32 实例材质位置) {
	auto* 绘图配置 = f_vk_get绘图环境(*m_Ctx);
	auto* 绘图框架 = f_vk_get绘图框架(*m_Ctx);

	VkAccelerationStructureGeometryKHR 几何体{ VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_GEOMETRY_KHR };
	几何体.flags = VK_GEOMETRY_OPAQUE_BIT_KHR | VK_GEOMETRY_NO_DUPLICATE_ANY_HIT_INVOCATION_BIT_KHR;
	几何体.geometryType = VK_GEOMETRY_TYPE_TRIANGLES_KHR;
	几何体.geometry.triangles.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_GEOMETRY_TRIANGLES_DATA_KHR;
	几何体.geometry.triangles.vertexFormat = VK_FORMAT_R32G32B32_SFLOAT;
	几何体.geometry.triangles.vertexStride = sizeof(S_VNT1);
	几何体.geometry.triangles.indexType = VK_INDEX_TYPE_UINT32;
	
	几何体.geometry.triangles.vertexData.deviceAddress = f_buf_getOffsetAddress(*(me->m_顶点));
	几何体.geometry.triangles.indexData.deviceAddress = f_buf_getOffsetAddress(*(me->m_索引));
	几何体.geometry.triangles.maxVertex = me->m_顶点->m_Mem.m_数量;

	

	assert(me->m_顶点->m_Mem.m_数量 > 0);
	
	
	uint32 图元数量 = 0;
	//m_几何图元数量.clear();
	VkAccelerationStructureBuildRangeInfoKHR* 加速体区间绑定 = (VkAccelerationStructureBuildRangeInfoKHR*)malloc(sizeof(VkAccelerationStructureBuildRangeInfoKHR));
	//static VkAccelerationStructureBuildRangeInfoKHR 加速体区间绑定{};
	
	switch (ObType) {
		case E_物体类型::t_多边形: 
		case E_物体类型::t_网格物体: {
			uint32 tnum = me->m_索引->m_Mem.m_数量 / 3;
			assert(tnum > 1);
			加速体区间绑定->firstVertex = 0;
			加速体区间绑定->primitiveCount = tnum;
			加速体区间绑定->primitiveOffset = 0;
			加速体区间绑定->transformOffset = 0;

			图元数量 = tnum;
			//m_几何图元数量.push_back(tnum);
			break;
		}
		case E_物体类型::t_区域光_方形: {
			加速体区间绑定->firstVertex = 0;
			加速体区间绑定->primitiveCount = 2;
			加速体区间绑定->primitiveOffset = 0;
			加速体区间绑定->transformOffset = 0;

			图元数量 = 2;
			//m_几何图元数量.push_back(2);
			break;
		}
		case E_物体类型::t_区域光_圆形: {
			加速体区间绑定->firstVertex = 0;
			加速体区间绑定->primitiveCount = 128;
			加速体区间绑定->primitiveOffset = 0;
			加速体区间绑定->transformOffset = 0;

			图元数量 = 128;
			//
			break;
		}
	}


	
	VkAccelerationStructureBuildGeometryInfoKHR 加速结构绑定几何信息 = { VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_BUILD_GEOMETRY_INFO_KHR };
	加速结构绑定几何信息.type = VK_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL_KHR;
	加速结构绑定几何信息.flags = VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_TRACE_BIT_KHR | VK_BUILD_ACCELERATION_STRUCTURE_ALLOW_UPDATE_BIT_KHR;
	加速结构绑定几何信息.geometryCount = 1;
	加速结构绑定几何信息.pGeometries = &几何体;

	VkAccelerationStructureBuildSizesInfoKHR 加速结构绑定大小信息 = { VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_BUILD_SIZES_INFO_KHR };
	绘图框架->vkGetAccelerationStructureBuildSizes(绘图配置->m_设备,
												VK_ACCELERATION_STRUCTURE_BUILD_TYPE_DEVICE_KHR,
												&加速结构绑定几何信息,
												&图元数量,
												&加速结构绑定大小信息);
	
	
	//static std::mutex 底层加速结构准备锁;
	//底层加速结构准备锁.lock();
	uint32 ass = (uint32(加速结构绑定大小信息.accelerationStructureSize / 256) + 1) * 256;
	//uint32 ass = 加速结构绑定大小信息.accelerationStructureSize;
	f_buf_resize(m_底层暂存缓冲区, ass);
	f_bm_resize(*me->m_底层光追加速结构, ass);
	
	
	//f_mesh_构建网格属性参数(ob, me, DEF_ObType_Mesh);


	S_Geom geom{};// = me->m_几何属性;
	geom.uvIndexOffset012 = me->m_几何属性.uvIndexOffset012;
	geom.uvIndexOffset345 = me->m_几何属性.uvIndexOffset345;
	geom.uvIndexOffset678 = me->m_几何属性.uvIndexOffset678;
	
	me->m_几何属性.boundSize = f_me_getBoundSize(me);
	me->m_几何属性.boundOffset = me->m_Bound->min;

	geom.boundSize = f_me_getBoundSize(me);
	geom.boundOffset = f_me_getBound(me).min;
	if (me->m_UV->m_Mem.m_数量) {
		geom.uvLayerAndOffset.y = me->m_UV->m_Mem.m_偏移;
		geom.uvLayerAndOffset.x = me->m_几何属性.uvLayerAndOffset.x;
		//geom.uvLayerAndOffset.x = 0;
	}
	else {
		geom.uvLayerAndOffset = {};
	}
	if (id >= f_buf_getGlobal().m_几何属性->m_Size) f_buf_resize(f_buf_getGlobal().m_几何属性, id + 1);
	f_buf_set<S_Geom>(f_buf_getGlobal().m_几何属性, (uint64)id, geom);



	//底层加速结构准备锁.unlock();
	if (me->m_更新光追加速结构) {
		加速结构绑定几何信息.mode = VK_BUILD_ACCELERATION_STRUCTURE_MODE_BUILD_KHR;
		//m_加速结构绑定几何信息.dstAccelerationStructure = f_buf_get加速结构(BLAS);
		//加速结构绑定几何信息.dstAccelerationStructure = (VkAccelerationStructureKHR)f_buf_get加速结构(me->m_底层光追加速结构->m_Buf);
		加速结构绑定几何信息.dstAccelerationStructure = (VkAccelerationStructureKHR)f_buf_get加速结构(*me->m_底层光追加速结构);
		加速结构绑定几何信息.scratchData.deviceAddress = f_buf_getAddress(static_cast<S_VkArray缓存*>(m_底层暂存缓冲区));


		std::cout << "开始光追底层加速结构" << std::endl;
		VkAccelerationStructureBuildRangeInfoKHR* 区间绑定 = 加速体区间绑定;
		f_Draw_Begin(m_Ctx, g_加速结构创建命令缓存, g_渲染Fence);
		绘图框架->vkCmdBuildAccelerationStructures((VkCommandBuffer)g_加速结构创建命令缓存, 1, &加速结构绑定几何信息, &区间绑定);
		f_Draw_end(m_Ctx, g_加速结构创建命令缓存, g_渲染Fence, S_光追后端::g_光追列队);
		std::cout << "结束光追底层加速结构" << std::endl;

		me->m_更新光追加速结构 = false;
	}
	//free(加速体区间绑定);
	//std::cout<<"光追底层加速结构构建完成:"<< id << std::endl;
}

void C_Vk光追后端::f_构建底层加速结构() {
	auto* 绘图配置 = f_vk_get绘图环境(*m_Ctx);
	auto* 绘图框架 = f_vk_get绘图框架(*m_Ctx);
	
	uint32 num = m_底层加速结构网格.size();

	m_底层暂存缓冲区->f_resize(DEF_Max(m_底层暂存缓冲区->elt_capacity, m_底层加速结构内存分配大小));
	f_buf_getGlobal().g_BLAS_0->f_resize(DEF_Max(f_buf_getGlobal().g_BLAS_0->elt_capacity, m_底层加速结构内存分配大小));
	std::cout << "开始分配底层加速结构内存:" << m_底层加速结构内存分配大小 << std::endl;
	for (uint32 i = 0; i < num; ++i) {
		S_Mesh& me = *m_底层加速结构网格[i];

		m_底层暂存缓冲区内存块.push_back(f_bm_alloc(m_底层暂存缓冲区, m_几何图元数量[i]));
		f_bm_resize(*me.m_底层光追加速结构, m_几何图元数量[i]);
	}

	
	for (uint32 i = 0; i < num; ++i) {
		S_Mesh& me = *m_底层加速结构网格[i];

		m_加速结构绑定几何信息[i].geometryCount = 1;
		//m_加速结构绑定几何信息[i].pGeometries = &m_几何加速结构[i];
		m_加速结构绑定几何信息[i].mode = VK_BUILD_ACCELERATION_STRUCTURE_MODE_BUILD_KHR;
		//m_加速结构绑定几何信息[i].dstAccelerationStructure = (VkAccelerationStructureKHR)f_buf_get加速结构(*me.m_底层光追加速结构);
		m_加速结构绑定几何信息[i].dstAccelerationStructure = f_buf_get加速结构地址(me.m_底层光追加速结构->m_Buf);
		m_加速结构绑定几何信息[i].scratchData.deviceAddress = f_buf_getOffsetAddress(m_底层暂存缓冲区内存块[i]);

	}

	std::cout << "开始构建加速结构" << std::endl;
	f_Draw_Begin(m_Ctx, g_加速结构创建命令缓存, g_渲染Fence);
	绘图框架->vkCmdBuildAccelerationStructures((VkCommandBuffer)g_加速结构创建命令缓存
												, m_加速结构绑定几何信息.size()
												, m_加速结构绑定几何信息.data()
												, m_底层加速结构绑定信息.data());
	f_Draw_end(m_Ctx, g_加速结构创建命令缓存, g_渲染Fence, 3);

	std::cout << "光追底层加速结构构建完成" << std::endl;
	f_清除几何加速结构();

}

void C_Vk光追后端::f_清除几何加速结构() {
	for (auto& e : m_底层暂存缓冲区内存块) {
		f_bm_erase(e);
	}
	m_底层暂存缓冲区内存块.clear();

	for (auto& e : m_底层加速结构绑定信息) {
		free(e);
	}
	m_底层加速结构绑定信息.clear();

	m_加速结构绑定几何信息.clear();
	m_加速结构绑定大小信息.clear();
	m_底层加速结构网格.clear();
	//m_几何加速结构.clear();


	m_底层加速结构内存分配大小 = 0;
}

void C_Vk光追后端::f_清除顶层加速构建写入描述() {
	m_加速结构布局.clear();
	m_实例加速结构.clear();
	m_顶层加速结构内存块.clear();
}

void C_Vk光追后端::f_分配底层加速结构数量(uint32 num) {
	auto* 绘图配置 = f_vk_get绘图环境(*m_Ctx);
}

bool C_Vk光追后端::f_预分配实例变换(uint32 预分配) {
	if (m_顶层实例加速结构->m_Size < 预分配) {
		f_buf_resize(m_顶层实例加速结构, 预分配);
		f_bm_resize(m_RtOb, 预分配);
	}
	return false;
}

bool C_Vk光追后端::f_更新实例变换(S_GPU内存块& BLAS, mat3X4 mat, uint32 offset, uint32 底层ASID, uint8 实例材质ID, uint8 mask, uint32 预分配) {
	//auto* 绘图配置 = f_vk_get绘图环境(*m_Ctx);
	if (offset >= m_顶层实例加速结构->m_Size) {
		f_buf_resize(m_顶层实例加速结构, offset + 1);
	}

	if (offset >= m_RtOb.m_Mem.m_数量) {
		f_bm_resize(m_RtOb, offset + 1);
	}


	auto ptr_instance = f_buf_map板载缓存<VkAccelerationStructureInstanceKHR>(m_顶层实例加速结构);
	ptr_instance[offset].transform = *((VkTransformMatrixKHR*)&mat);
	ptr_instance[offset].instanceCustomIndex = 底层ASID | (uint32(实例材质ID) << DEF_RY_InstanceBitSize);
	ptr_instance[offset].mask = mask;
	ptr_instance[offset].instanceShaderBindingTableRecordOffset = 0;
	ptr_instance[offset].flags = VK_GEOMETRY_INSTANCE_TRIANGLE_FACING_CULL_DISABLE_BIT_KHR;
	//ptr_instance[offset].accelerationStructureReference = f_buf_getAddress(static_cast<S_VkArray缓存*>(m_底层加速结构缓存组[底层ASID]));
	ptr_instance[offset].accelerationStructureReference = f_buf_getOffsetAddress(BLAS);
	f_buf_unmap板载缓存(m_顶层实例加速结构);

	auto& instanceOb = f_bm_at<S_RtOb>(m_RtOb, offset);
	instanceOb.m_Address_InstanceAttrIndex = 0;
	instanceOb.m_AddressMesh = 0;
	instanceOb.m_MaterialID = 0;
	instanceOb.m_InstanceID = offset;
	instanceOb.m_SceneMask_ObMask = 0;

	return true;
}

void C_Vk光追后端::f_构建顶层加速结构(uint32 num) {
	auto* 绘图环境 = f_vk_get绘图环境(*m_Ctx);
	auto* 绘图框架 = f_vk_get绘图框架(*m_Ctx);
	//auto* 绘图配置 = (S_VK绘图设备*)m_绘图环境;

	*f_buf_全局GPU参数指针_ptr(f_buf_getGlobal().g_全局绑定参数) = f_df_全局GPU参数();

	m_mapPtr_渲染帧数据->m_PointLightNum = f_buf_getGlobal().m_点光源缓冲区->m_Size;
	//uint32 实例数量 = DEF_Min(10, num);
	uint32 实例数量 = num;
	VkDeviceOrHostAddressConstKHR 实例数据{};
	

	if (num) {
		实例数据.deviceAddress = f_buf_getAddress(m_顶层实例加速结构);
		m_顶层几何实例加速结构.clear();
		VkAccelerationStructureGeometryKHR asg{ VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_GEOMETRY_KHR }; {
			asg.geometryType = VK_GEOMETRY_TYPE_INSTANCES_KHR;
			asg.flags = VK_GEOMETRY_OPAQUE_BIT_KHR;
			//asg.flags = VK_GEOMETRY_NO_DUPLICATE_ANY_HIT_INVOCATION_BIT_KHR;
			asg.geometry.instances.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_GEOMETRY_INSTANCES_DATA_KHR;
			asg.geometry.instances.arrayOfPointers = VK_FALSE;
			//asg.geometry.instances.arrayOfPointers = true;
			asg.geometry.instances.data = 实例数据;
		}
		m_顶层几何实例加速结构.push_back(asg);


		m_加速结构绑定几何实例信息 = { VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_BUILD_GEOMETRY_INFO_KHR };
		m_加速结构绑定几何实例信息.type = VK_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL_KHR;
		m_加速结构绑定几何实例信息.flags = VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_TRACE_BIT_KHR;
		m_加速结构绑定几何实例信息.geometryCount = m_顶层几何实例加速结构.size();
		m_加速结构绑定几何实例信息.pGeometries = m_顶层几何实例加速结构.data();



		uint32_t primitive_count = 实例数量;
		//uint32_t primitive_count = 1;
		m_加速结构绑定实例大小信息 = { VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_BUILD_SIZES_INFO_KHR };
		绘图框架->vkGetAccelerationStructureBuildSizes(
			绘图环境->m_设备,
			VK_ACCELERATION_STRUCTURE_BUILD_TYPE_DEVICE_KHR,
			&m_加速结构绑定几何实例信息,
			&primitive_count,
			&m_加速结构绑定实例大小信息);

		f_buf_resize(m_顶层加速结构,   m_加速结构绑定实例大小信息.accelerationStructureSize);
		f_buf_resize(m_顶层暂存缓冲区, m_加速结构绑定实例大小信息.accelerationStructureSize);

		m_加速结构绑定几何实例信息.type = VK_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL_KHR;
		m_加速结构绑定几何实例信息.flags = VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_TRACE_BIT_KHR;
		m_加速结构绑定几何实例信息.mode = VK_BUILD_ACCELERATION_STRUCTURE_MODE_BUILD_KHR;
		//m_加速结构绑定几何实例信息.mode = VK_BUILD_ACCELERATION_STRUCTURE_MODE_UPDATE_KHR;
		m_加速结构绑定几何实例信息.dstAccelerationStructure = f_buf_get加速结构地址(m_顶层加速结构);
		m_加速结构绑定几何实例信息.scratchData.deviceAddress = f_buf_getAddress(static_cast<S_VkArray缓存*>(m_顶层暂存缓冲区));


		m_实例区间.clear();
		VkAccelerationStructureBuildRangeInfoKHR 实例区间{};
		实例区间.primitiveCount = 实例数量;
		//实例区间.primitiveCount = 1;
		实例区间.primitiveOffset = 0;
		实例区间.firstVertex = 0;
		实例区间.transformOffset = 0;
		m_实例区间.push_back(&实例区间);



		f_Draw_Begin(m_Ctx, g_加速结构创建命令缓存, g_渲染Fence);
		绘图框架->vkCmdBuildAccelerationStructures((VkCommandBuffer)g_加速结构创建命令缓存, 1, &m_加速结构绑定几何实例信息, m_实例区间.data());
		f_Draw_end(m_Ctx, g_加速结构创建命令缓存, g_渲染Fence, S_光追后端::g_光追列队);


		return;
	}
	else {
		实例数据.deviceAddress = 0;
		m_顶层几何实例加速结构.clear();
		VkAccelerationStructureGeometryKHR asg{ VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_GEOMETRY_KHR }; {
			asg.geometryType = VK_GEOMETRY_TYPE_INSTANCES_KHR;
			asg.flags = VK_GEOMETRY_OPAQUE_BIT_KHR;
			asg.geometry.instances.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_GEOMETRY_INSTANCES_DATA_KHR;
			asg.geometry.instances.arrayOfPointers = VK_FALSE;
			asg.geometry.instances.data = 实例数据;
		}
		m_顶层几何实例加速结构.push_back(asg);


		m_加速结构绑定几何实例信息 = { VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_BUILD_GEOMETRY_INFO_KHR };
		m_加速结构绑定几何实例信息.type = VK_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL_KHR;
		m_加速结构绑定几何实例信息.flags = VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_TRACE_BIT_KHR;
		m_加速结构绑定几何实例信息.geometryCount = 1;
		m_加速结构绑定几何实例信息.pGeometries = m_顶层几何实例加速结构.data();
		m_加速结构绑定几何实例信息.dstAccelerationStructure = f_buf_get加速结构地址(m_顶层加速结构);
		m_加速结构绑定几何实例信息.scratchData.deviceAddress = f_buf_getAddress(static_cast<S_VkArray缓存*>(m_顶层暂存缓冲区));

		m_实例区间.clear();
		VkAccelerationStructureBuildRangeInfoKHR 实例区间{};
		实例区间.primitiveCount = 0;
		实例区间.primitiveOffset = 0;
		实例区间.firstVertex = 0;
		实例区间.transformOffset = 0;
		m_实例区间.push_back(&实例区间);
	}
	

	f_Draw_Begin(m_Ctx, g_加速结构创建命令缓存, g_渲染Fence);
	绘图框架->vkCmdBuildAccelerationStructures((VkCommandBuffer)g_加速结构创建命令缓存, 1, &m_加速结构绑定几何实例信息, m_实例区间.data());
	f_Draw_end(m_Ctx, g_加速结构创建命令缓存, g_渲染Fence, S_光追后端::g_光追列队);


}


void C_Vk光追后端::f_构建着色绑定列表() {
	auto* 绘图环境 = f_vk_get绘图环境(*m_Ctx);
	auto* 绘图框架 = f_vk_get绘图框架(*m_Ctx);
	//auto* 绘图配置 = (S_VK绘图设备*)m_绘图环境;

	auto rt = static_cast<S_Vk光追线管*>(m_光追管线);
	uint32 着色器数量 = rt->m_着色->m_光追着色器.size();
	uint32 着色组列表大小 = m_光追管线属性.shaderGroupHandleSize;
	
	uint32 光线生成对齐跨度 = f_shader_着色组对齐大小(着色组列表大小, m_光追管线属性.shaderGroupHandleAlignment);



	std::vector<uint32> 着色器阶段数量(5);
	std::vector<uint32> 着色器列表阶段偏移(5);
	uint32 offset = 0;
	for (uint32 i = 0; i < 着色器数量; ++i) {
		switch (rt->m_着色->m_光追着色器[i].m_Type) {
		case E_着色阶段::e_光追射线生成:
			if(!着色器列表阶段偏移[E_光追阶段ID::e_光追阶段ID_光线生成]) 着色器列表阶段偏移[E_光追阶段ID::e_光追阶段ID_光线生成] = offset;
			++着色器阶段数量[E_光追阶段ID::e_光追阶段ID_光线生成];
			break;
		case E_着色阶段::e_光追忽略相交:
			if(!着色器列表阶段偏移[E_光追阶段ID::e_光追阶段ID_光线忽略]) 着色器列表阶段偏移[E_光追阶段ID::e_光追阶段ID_光线忽略] = offset;
			++着色器阶段数量[E_光追阶段ID::e_光追阶段ID_光线忽略];
			break;
		case E_着色阶段::e_光追最近相交:
			if(!着色器列表阶段偏移[E_光追阶段ID::e_光追阶段ID_最近相交]) 着色器列表阶段偏移[E_光追阶段ID::e_光追阶段ID_最近相交] = offset;
			++着色器阶段数量[E_光追阶段ID::e_光追阶段ID_最近相交];
			break;
		//case E_着色阶段::e_光追AABB:
		//	if (!着色器列表阶段偏移[E_光追阶段ID::e_光追阶段ID_AABB]) 着色器列表阶段偏移[E_光追阶段ID::e_光追阶段ID_AABB] = offset;
		//	++着色器阶段数量[E_光追阶段ID::e_光追阶段ID_AABB];
		//	break;
		case E_着色阶段::e_光追回调:
			if(!着色器列表阶段偏移[E_光追阶段ID::e_光追阶段ID_材质回调]) 着色器列表阶段偏移[E_光追阶段ID::e_光追阶段ID_材质回调] = offset;
			++着色器阶段数量[E_光追阶段ID::e_光追阶段ID_材质回调];
			break;
		default:
			continue;
		}
		++offset;
	}
	//offset *= 着色组列表大小;
	着色组列表大小 *= offset;

	std::vector<uint8> shaderHandleStorage(着色组列表大小);
	VkResult err = 绘图框架->vkGetRayTracingShaderGroupHandles(绘图环境->m_设备, rt->m_线管, 0, offset, 着色组列表大小, shaderHandleStorage.data());
	if (err != VK_SUCCESS) {
		std::cout << "error: vkGetRayTracingShaderGroupHandles " << 着色器数量 << " : " << 着色组列表大小 << std::endl;
	}
	
	uint32 回调数量 = 着色器阶段数量[E_光追阶段ID::e_光追阶段ID_材质回调];
	uint32 对齐跨度 = f_shader_着色组对齐大小(m_光追管线属性.shaderGroupHandleSize, m_光追管线属性.shaderGroupHandleAlignment);

	uint32 光线生成列表大小 = f_shader_着色组对齐大小(对齐跨度 * 着色器阶段数量[E_光追阶段ID::e_光追阶段ID_光线生成], m_光追管线属性.shaderGroupBaseAlignment);
	uint32 光线忽略列表大小 = f_shader_着色组对齐大小(对齐跨度 * 着色器阶段数量[E_光追阶段ID::e_光追阶段ID_光线忽略], m_光追管线属性.shaderGroupBaseAlignment);
	//uint32 光线AABB列表大小 = f_shader_着色组对齐大小(对齐跨度 * 着色器阶段数量[E_光追阶段ID::e_光追阶段ID_AABB], m_光追管线属性.shaderGroupBaseAlignment);
	uint32 光线相交列表大小 = f_shader_着色组对齐大小(对齐跨度 * 着色器阶段数量[E_光追阶段ID::e_光追阶段ID_最近相交], m_光追管线属性.shaderGroupBaseAlignment);
	//uint32 光线回调列表大小 = f_shader_着色组对齐大小(对齐跨度 * 回调数量, m_光追管线属性.shaderGroupBaseAlignment);


	f_buf_resize(m_生成着色列表, 光线生成列表大小);
	f_buf_resize(m_忽略着色列表, 光线忽略列表大小);
	f_buf_resize(m_相交着色列表, 光线相交列表大小);
	//f_buf_resize(m_AABB着色列表, 光线相交列表大小);
	f_buf_set(m_生成着色列表, 0);
	f_buf_set(m_忽略着色列表, 0);
	f_buf_set(m_相交着色列表, 0);
	//f_buf_set(m_AABB着色列表, 0);
	

	offset = 0;
	f_buf_fill(m_生成着色列表, 0, &shaderHandleStorage[对齐跨度 * E_光追阶段ID::e_光追阶段ID_光线生成], 对齐跨度);
	offset += 光线生成列表大小;
	f_buf_fill(m_忽略着色列表, 0, &shaderHandleStorage[对齐跨度 * E_光追阶段ID::e_光追阶段ID_光线忽略], 对齐跨度);
	offset += 光线忽略列表大小;
	//f_buf_fill(m_AABB着色列表, 0, &shaderHandleStorage[对齐跨度 * E_光追阶段ID::e_光追阶段ID_AABB], 对齐跨度);
	//offset += 光线忽略列表大小;
	f_buf_fill(m_相交着色列表, 0, &shaderHandleStorage[对齐跨度 * E_光追阶段ID::e_光追阶段ID_最近相交], 对齐跨度);
	
	

	offset = 0;
	if (回调数量) {
		f_buf_resize(m_回调着色列表, 光线生成列表大小 * 回调数量);
		
		for (uint32 i = 0; i < 回调数量; ++i) {
			f_buf_fill(m_回调着色列表, offset, &shaderHandleStorage[对齐跨度 * (E_光追阶段ID::e_光追阶段ID_材质回调 + i)], 对齐跨度);
			offset += 光线生成列表大小;
		}
	}


	auto 地址 = f_shader_get着色绑定列表跨度设备地址((S_VkArray缓存*)m_生成着色列表, 光线生成列表大小, 光线生成列表大小, 0);
	m_光追生成GPU地址 = 地址;

	地址 = f_shader_get着色绑定列表跨度设备地址((S_VkArray缓存*)m_忽略着色列表, 光线生成列表大小, 光线生成列表大小, 0);
	m_光追忽略GPU地址 = 地址;

	//地址 = f_shader_get着色绑定列表跨度设备地址((S_VkArray缓存*)m_AABB着色列表, 光线生成列表大小, 光线生成列表大小, 0);
	//m_光追AABBGPU地址 = 地址;

	地址 = f_shader_get着色绑定列表跨度设备地址((S_VkArray缓存*)m_相交着色列表, 光线生成列表大小, 光线生成列表大小, 0);
	m_光追相交GPU地址 = 地址;

	if (回调数量) {
		地址 = f_shader_get着色绑定列表跨度设备地址((S_VkArray缓存*)m_回调着色列表, 光线生成列表大小, 光线生成列表大小*回调数量, 0);
		m_光追回调GPU地址 = 地址;
	}
	else {
		m_光追回调GPU地址 = {};
	}
	

	rt->m_着色->f_alloc参数设置((S_结构指针*)&m_GPU参数设置);
	std::cout<<"build shaderGroupHandle" << std::endl;
}

void C_Vk光追后端::f_构建GPU参数布局(S_纹理& tex) {
	
	S_GlobalParam 全局参数 = f_df_全局GPU参数();
	//f_buf_set<S_GlobalParam>(S_全局缓存::m_全局绑定参数指针, 0, 全局参数);
	*f_buf_全局GPU参数指针_ptr(f_buf_getGlobal().g_全局绑定参数) = 全局参数;


	m_mapPtr_渲染帧数据->m_PointLightNum = f_buf_getGlobal().m_点光源缓冲区->m_Size;

	uint32 num = 1;
	std::vector<VkWriteDescriptorSetAccelerationStructureKHR> 加速结构布局(1);
	std::vector<VkAccelerationStructureKHR> AccelerationStructure(1);
	for (uint32 i = 0; i < num; ++i) {
		AccelerationStructure[i] = f_buf_get加速结构地址(m_顶层加速结构);
	}

	for(uint32 i = 0; i < num; ++i){
		VkWriteDescriptorSetAccelerationStructureKHR info{ VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET_ACCELERATION_STRUCTURE_KHR };

		info.pAccelerationStructures = &AccelerationStructure[i];
		info.accelerationStructureCount = AccelerationStructure.size();

		加速结构布局[i] = info;
	}


	m_参数布局.m_参数槽[0] = 加速结构布局.data();
	m_参数布局.m_参数类型[0] = E_板载缓存类型::e_光追BVH;
	m_参数布局.m_绑定位置[0] = 0;
	m_参数布局.m_绑定数量[0] = AccelerationStructure.size();

	m_参数布局.m_参数槽[1] = &tex;
	m_参数布局.m_参数类型[1] = E_板载缓存类型::e_IMAGE;
	m_参数布局.m_绑定位置[1] = DEF_BINDING_Render_Image;
	m_参数布局.m_绑定数量[1] = 1;

	m_参数布局.m_参数槽[2] = m_投影矩阵;
	m_参数布局.m_参数类型[2] = E_板载缓存类型::e_SSBO;
	m_参数布局.m_绑定位置[2] = DEF_BINDING_Render_Camera;
	m_参数布局.m_绑定数量[2] = 1;

	m_参数布局.m_参数槽[3] = f_buf_getGlobal().m_场景物体网格属性;
	m_参数布局.m_参数类型[3] = E_板载缓存类型::e_SSBO;
	m_参数布局.m_绑定位置[3] = DEF_ObMeshBindID;
	m_参数布局.m_绑定数量[3] = 1;

	m_参数布局.m_参数槽[4] = f_buf_getGlobal().g_材质;
	m_参数布局.m_参数类型[4] = E_板载缓存类型::e_SSBO;
	m_参数布局.m_绑定位置[4] = DEF_MaterialsBindID;
	m_参数布局.m_绑定数量[4] = 1;


	m_参数布局.m_参数槽[5] = m_帧渲染信息;
	m_参数布局.m_参数类型[5] = E_板载缓存类型::e_UBO;
	m_参数布局.m_绑定位置[5] = DEF_RenderFrameBindID;
	m_参数布局.m_绑定数量[5] = 1;

	m_参数布局.m_参数槽[6] = f_buf_getGlobal().m_平行光缓冲区;
	m_参数布局.m_参数类型[6] = E_板载缓存类型::e_SSBO;
	m_参数布局.m_绑定位置[6] = DEF_ParallelLightBindID;
	m_参数布局.m_绑定数量[6] = 1;

	m_参数布局.m_参数槽[7] = f_get纹理(E_纹理分组类型::e_2D纹理);
	m_参数布局.m_参数类型[7] = E_板载缓存类型::e_SAMPLER_Array;
	m_参数布局.m_绑定位置[7] = DEF_TexturesBindID;
	m_参数布局.m_绑定数量[7] = f_get纹理数量(E_纹理分组类型::e_2D纹理);

	m_参数布局.m_参数槽[8] = f_buf_getGlobal().m_几何属性;
	m_参数布局.m_参数类型[8] = E_板载缓存类型::e_SSBO;
	m_参数布局.m_绑定位置[8] = DEF_BINDING_Render_Geom;
	m_参数布局.m_绑定数量[8] = 1;

	m_参数布局.m_参数槽[9] = f_buf_getGlobal().m_全局绑定参数指针;
	m_参数布局.m_参数类型[9] = E_板载缓存类型::e_SSBO;
	m_参数布局.m_绑定位置[9] = DEF_BINDING_Render_GlobalParam;
	m_参数布局.m_绑定数量[9] = 1;

	m_参数布局.m_参数槽[10] = f_get纹理(E_纹理分组类型::e_字符纹理);
	m_参数布局.m_参数类型[10] = E_板载缓存类型::e_SAMPLER_Array;
	m_参数布局.m_绑定位置[10] = DEF_BINDING_RayArrayTexturesBindID;
	m_参数布局.m_绑定数量[10] = f_get纹理数量(E_纹理分组类型::e_字符纹理);


	//m_参数布局.m_参数槽[11] = f_get纹理(S_光追后端::E_纹理分组类型::e_渲染输出层);
	//m_参数布局.m_参数类型[11] = E_板载缓存类型::e_IMAGE_Array;
	//m_参数布局.m_绑定位置[11] = DEF_BINDING_RayTexturesWriteLayers;
	//m_参数布局.m_绑定数量[11] = f_get纹理数量(S_光追后端::E_纹理分组类型::e_渲染输出层);


	f_df_setGPU参数槽(m_Ctx->m_逻辑设备.设备, m_GPU参数设置, m_参数布局);
}

void C_Vk光追后端::f_构建BVH() {
	auto* 绘图配置 = f_vk_get绘图环境(*m_Ctx);
}

void C_Vk光追后端::f_填充平行光参数(std::vector<S_LightParallel>& 平行光参数组) {
	if (平行光参数组.size()) {
		f_buf_resize(f_buf_getGlobal().m_平行光缓冲区, 平行光参数组.size());
		f_buf_fill(f_buf_getGlobal().m_平行光缓冲区, 平行光参数组.data(), 平行光参数组.size());
	}
	
	m_mapPtr_渲染帧数据->m_ParallelLightNum = 平行光参数组.size();
	
}

void C_Vk光追后端::f_Rander(GLSL_RenderFrame& 帧信息) {
	auto* 绘图环境 = f_vk_get绘图环境(*m_Ctx);
	auto* 绘图框架 = f_vk_get绘图框架(*m_Ctx);
	//f_递增渲染帧记录();
	//auto* 绘图配置 = (S_VK绘图设备*)m_绘图环境;

	//(*m_mapPtr_渲染帧数据) = 帧信息;
	//(*m_mapPtr_渲染帧数据).m_world_ShaderID = m_渲染配置.环境材质ID;
	//(*m_mapPtr_渲染帧数据).m_PointLightNum = m_渲染配置.点光源数量;


	VkDescriptorSet DS = (VkDescriptorSet)m_GPU参数设置;

	f_Draw_Begin(m_Ctx, m_渲染命令缓存, g_渲染Fence);

	//f_更新纹理布局();

	vkCmdBindPipeline((VkCommandBuffer)m_渲染命令缓存, VK_PIPELINE_BIND_POINT_RAY_TRACING_KHR, static_cast<S_Vk光追线管*>(m_光追管线)->m_线管);
	vkCmdBindDescriptorSets((VkCommandBuffer)m_渲染命令缓存, VK_PIPELINE_BIND_POINT_RAY_TRACING_KHR, static_cast<S_Vk光追线管*>(m_光追管线)->m_线管布局, 0, 1, &DS, 0, 0);


	VkStridedDeviceAddressRegionKHR emptySbtEntry = {};
	/*绘图配置->vkCmdTraceRays((VkCommandBuffer)m_渲染命令缓存,
		&(rt->m_着色->m_光追着色器[0].m_跨度设备指针),
		&(rt->m_着色->m_光追着色器[1].m_跨度设备指针),
		m_光追相交GPU地址.data(),
		m_光追回调GPU地址.data(),
		viewSize.x,
		viewSize.y,
		1
	);*/

	绘图框架->vkCmdTraceRays((VkCommandBuffer)m_渲染命令缓存,
						&m_光追生成GPU地址,
						&m_光追忽略GPU地址,
						&m_光追相交GPU地址,
						&m_光追回调GPU地址,
						//&emptySbtEntry,
						m_瓦片大小.x,
						m_瓦片大小.y,
						1
					);


	f_Draw_end(m_Ctx, m_渲染命令缓存, g_渲染Fence, 3);

	f_递增渲染帧记录();
}

void C_Vk光追后端::f_Rander(GLSL_RenderFrame& 帧信息, uint32 采样次数) {
	auto* 绘图环境 = f_vk_get绘图环境(*m_Ctx);
	auto* 绘图框架 = f_vk_get绘图框架(*m_Ctx);
	//auto* 绘图配置 = (S_VK绘图设备*)m_绘图环境;

	//(*m_mapPtr_渲染帧数据) = 帧信息;
	//(*m_mapPtr_渲染帧数据).m_world_ShaderID = m_渲染配置.环境材质ID;
	//(*m_mapPtr_渲染帧数据).m_PointLightNum = m_渲染配置.点光源数量;


	VkDescriptorSet DS = (VkDescriptorSet)m_GPU参数设置;

	if (采样次数) {
		std::cout << "瓦块X:" << m_渲染配置.m_瓦片ID.x << " 瓦块Y:" << m_渲染配置.m_瓦片ID.y << "\n";
	}
	std::cout << "完成渲染采样:";
	for (uint32 i = 0; i < 采样次数; ++i) {
		f_递增渲染帧记录();

		f_Draw_Begin(m_Ctx, m_渲染命令缓存, g_渲染Fence);

		//f_更新纹理布局();

		vkCmdBindPipeline((VkCommandBuffer)m_渲染命令缓存, VK_PIPELINE_BIND_POINT_RAY_TRACING_KHR, static_cast<S_Vk光追线管*>(m_光追管线)->m_线管);
		vkCmdBindDescriptorSets((VkCommandBuffer)m_渲染命令缓存, VK_PIPELINE_BIND_POINT_RAY_TRACING_KHR, static_cast<S_Vk光追线管*>(m_光追管线)->m_线管布局, 0, 1, &DS, 0, 0);

		VkStridedDeviceAddressRegionKHR emptySbtEntry = {};
		
		绘图框架->vkCmdTraceRays((VkCommandBuffer)m_渲染命令缓存,
							&m_光追生成GPU地址,
							&m_光追忽略GPU地址,
							&m_光追相交GPU地址,
							&m_光追回调GPU地址,
							//&emptySbtEntry,
							 m_瓦片大小.x,
							 m_瓦片大小.y,
							1
		);

		f_Draw_end(m_Ctx, m_渲染命令缓存, g_渲染Fence, 3);

		if (采样次数) {
			std::cout<< 采样次数 << "/" << i << " ";
		}

		if(!S_光追后端::g_渲染状态) break;
	}
	std::cout << "\n";
}

void C_Vk光追后端::f_重构材质线管() {
	auto* 绘图环境 = f_vk_get绘图环境(*m_Ctx);
	//auto* 绘图框架 = f_vk_get绘图框架(*m_Ctx);
	//auto 绘图配置 = f_vk_get绘图环境(*m_Ctx);

	vkDestroyPipeline(绘图环境->m_设备, ((S_Vk光追线管*)m_光追管线)->m_线管, nullptr);

	f_vkShader_构建着色器组信息(((S_Vk光追线管*)m_光追管线)->m_着色);
	((S_Vk光追线管*)m_光追管线)->m_着色->f_initRT着色参数布局(m_线管创建配置信息);
	
	//vkDestroyPipelineLayout(绘图配置->m_设备, m_线管布局, nullptr);
	((S_Vk光追线管*)m_光追管线)->m_线管 = f_pipeline_创建(((S_Vk光追线管*)m_光追管线)->m_着色, ((S_Vk光追线管*)m_光追管线)->m_线管布局, *m_Ctx);

	f_构建着色绑定列表();
}

void C_Vk光追后端::f_重载() {
	auto* 绘图环境 = f_vk_get绘图环境(*m_Ctx);
	//auto* 绘图框架 = f_vk_get绘图框架(*m_Ctx);
	//auto* 绘图配置 = (S_VK绘图设备*)m_绘图环境;

	for (auto& e : ((S_Vk光追线管*)m_光追管线)->m_着色->m_着色阶段创建信息) {
		vkDestroyShaderModule(((S_Vk光追线管*)m_光追管线)->m_着色->m_设备, e.module, 0);
	}
	vkDestroyPipeline(绘图环境->m_设备, ((S_Vk光追线管*)m_光追管线)->m_线管, nullptr);

	((S_Vk光追线管*)m_光追管线)->m_线管 = f_pipeline_创建(((S_Vk光追线管*)m_光追管线)->m_着色, ((S_Vk光追线管*)m_光追管线)->m_线管布局, *m_Ctx);
	f_构建着色绑定列表();
}

void C_Vk光追后端::f_add纹理(S_纹理* 纹理, uint32 loc, E_纹理分组类型 分组) {
	switch (分组) {
		case E_纹理分组类型::e_2D纹理: {
			if (loc >= m_纹理信息.size()) {
				m_纹理信息.resize(loc + 1);
			}
			m_纹理信息[loc] = ((S_VkImage*)纹理)->m_纹理信息;
			break;
		}
			
		case E_纹理分组类型::e_字符纹理: {
			if (纹理->m_TexID >= m_字符纹理信息.size()) {
				m_字符纹理信息.resize(纹理->m_TexID + 1);
			}
			m_字符纹理信息[纹理->m_TexID] = ((S_VkImage*)纹理)->m_纹理信息;
			break;
		}

		case E_纹理分组类型::e_渲染输出层: {
			if (纹理->m_TexID >= m_渲染纹理层.size()) {
				m_渲染纹理层.resize(纹理->m_TexID + 1);
			}
			m_渲染纹理层[纹理->m_TexID] = ((S_VkImage*)纹理)->m_纹理信息;
			break;
		}
	}
}

void C_Vk光追后端::f_set纹理(S_纹理* 纹理, E_纹理分组类型 分组) {
	
	switch (分组) {
		case E_纹理分组类型::e_2D纹理: {
			m_纹理信息[纹理->m_TexID] = ((S_VkImage*)纹理)->m_纹理信息;
			break;
		}
		case E_纹理分组类型::e_字符纹理: {
			m_字符纹理信息[纹理->m_TexID] = ((S_VkImage*)纹理)->m_纹理信息;
			break;
		}
		case E_纹理分组类型::e_渲染输出层: {
			m_渲染纹理层[纹理->m_TexID] = ((S_VkImage*)纹理)->m_纹理信息;
			break;
		}
	}

}

void C_Vk光追后端::f_rem纹理(uint32 loc, E_纹理分组类型 分组) {
	switch (分组) {
		case E_纹理分组类型::e_2D纹理: {
			if (loc < m_纹理信息.size()) {
				m_纹理信息[loc] = m_纹理信息.back();
				m_纹理信息.pop_back();
			}
			break;
		}
		case E_纹理分组类型::e_字符纹理: {
			if (loc < m_字符纹理信息.size()) {
				m_字符纹理信息[loc] = m_字符纹理信息.back();
				m_字符纹理信息.pop_back();
			}
			break;
		}
		case E_纹理分组类型::e_渲染输出层: {
			if (loc < m_字符纹理信息.size()) {
				m_渲染纹理层[loc] = m_渲染纹理层.back();
				m_渲染纹理层.pop_back();
			}
			break;
		}
	}
}

void C_Vk光追后端::f_刷新纹理GPU布局() {
	m_渲染纹理层[E_RenderLayerType::e_renderLayer_composite]	= ((S_VkImage*)m_渲染纹理_最终合成)->m_纹理信息;
	m_渲染纹理层[E_RenderLayerType::e_renderLayer_diffuse]		= ((S_VkImage*)m_渲染纹理_漫反射)->m_纹理信息;
	m_渲染纹理层[E_RenderLayerType::e_renderLayer_depth]		= ((S_VkImage*)m_渲染纹理_深度)->m_纹理信息;
	m_渲染纹理层[E_RenderLayerType::e_renderLayer_normal]		= ((S_VkImage*)m_渲染纹理_法线)->m_纹理信息;
	m_渲染纹理层[E_RenderLayerType::e_renderLayer_illuminate]	= ((S_VkImage*)m_渲染纹理_自发光)->m_纹理信息;
	m_渲染纹理层[E_RenderLayerType::e_renderLayer_shadow]		= ((S_VkImage*)m_渲染纹理_光照)->m_纹理信息;



}

void C_Vk光追后端::f_更新纹理布局() {
	VkImageMemoryBarrier imageBarrier{};
	imageBarrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
	imageBarrier.oldLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
	imageBarrier.newLayout = VK_IMAGE_LAYOUT_GENERAL;
	imageBarrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
	imageBarrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
	
	imageBarrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
	imageBarrier.subresourceRange.baseMipLevel = 0;
	imageBarrier.subresourceRange.levelCount = 1;
	imageBarrier.subresourceRange.baseArrayLayer = 0;
	imageBarrier.subresourceRange.layerCount = 1;

	//imageBarrier.image = ((S_VkImage*)m_渲染纹理_最终合成)->m_Image;
	//vkCmdPipelineBarrier(
	//	(VkCommandBuffer)m_渲染命令缓存,
	//	VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
	//	VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
	//	0,
	//	0, nullptr,
	//	0, nullptr,
	//	1, &imageBarrier
	//);
	//
	//imageBarrier.image = ((S_VkImage*)m_渲染纹理_漫反射)->m_Image;
	//vkCmdPipelineBarrier(
	//	(VkCommandBuffer)m_渲染命令缓存,
	//	VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
	//	VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
	//	0,
	//	0, nullptr,
	//	0, nullptr,
	//	1, &imageBarrier
	//);
	//
	//imageBarrier.image = ((S_VkImage*)m_渲染纹理_深度)->m_Image;
	//vkCmdPipelineBarrier(
	//	(VkCommandBuffer)m_渲染命令缓存,
	//	VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
	//	VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
	//	0,
	//	0, nullptr,
	//	0, nullptr,
	//	1, &imageBarrier
	//);
	//
	//imageBarrier.image = ((S_VkImage*)m_渲染纹理_法线)->m_Image;
	//vkCmdPipelineBarrier(
	//	(VkCommandBuffer)m_渲染命令缓存,
	//	VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
	//	VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
	//	0,
	//	0, nullptr,
	//	0, nullptr,
	//	1, &imageBarrier
	//);
	//
	//imageBarrier.image = ((S_VkImage*)m_渲染纹理_自发光)->m_Image;
	//vkCmdPipelineBarrier(
	//	(VkCommandBuffer)m_渲染命令缓存,
	//	VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
	//	VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
	//	0,
	//	0, nullptr,
	//	0, nullptr,
	//	1, &imageBarrier
	//);
	//
	//imageBarrier.image = ((S_VkImage*)m_渲染纹理_光照)->m_Image;
	//vkCmdPipelineBarrier(
	//	(VkCommandBuffer)m_渲染命令缓存,
	//	VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
	//	VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
	//	0,
	//	0, nullptr,
	//	0, nullptr,
	//	1, &imageBarrier
	//);



	//for (auto& e : G纹理) {
	//	VkImageMemoryBarrier imageBarrier{};
	//	imageBarrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
	//	imageBarrier.oldLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
	//	imageBarrier.newLayout = VK_IMAGE_LAYOUT_GENERAL;
	//	imageBarrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
	//	imageBarrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
	//	imageBarrier.image = ((S_VkImage*)(S_纹理*)e.m_Ptr)->m_Image;
	//	imageBarrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
	//	imageBarrier.subresourceRange.baseMipLevel = 0;
	//	imageBarrier.subresourceRange.levelCount = 1;
	//	imageBarrier.subresourceRange.baseArrayLayer = 0;
	//	imageBarrier.subresourceRange.layerCount = 1;
	//	vkCmdPipelineBarrier(
	//		(VkCommandBuffer)m_渲染命令缓存,
	//		VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
	//		VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
	//		0,
	//		0, nullptr,
	//		0, nullptr,
	//		1, &imageBarrier
	//	);
	//}
	//for (auto& e : G字符纹理) {
	//	VkImageMemoryBarrier imageBarrier{};
	//	imageBarrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
	//	imageBarrier.oldLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
	//	imageBarrier.newLayout = VK_IMAGE_LAYOUT_GENERAL;
	//	imageBarrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
	//	imageBarrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
	//	imageBarrier.image = ((S_VkImage*)(S_纹理*)e.m_Ptr)->m_Image;
	//	imageBarrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
	//	imageBarrier.subresourceRange.baseMipLevel = 0;
	//	imageBarrier.subresourceRange.levelCount = 1;
	//	imageBarrier.subresourceRange.baseArrayLayer = 0;
	//	imageBarrier.subresourceRange.layerCount = 1;
	//	vkCmdPipelineBarrier(
	//		(VkCommandBuffer)m_渲染命令缓存,
	//		VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
	//		VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
	//		0,
	//		0, nullptr,
	//		0, nullptr,
	//		1, &imageBarrier
	//	);
	//}
}




void f_bd_安装光追材质着色器(S_光追后端* rt, int32 loc, std::string path, uint32 光追ID) {
	if (f_file_文件是否存在(path)) {
		auto* pipeline = ((S_Vk光追线管*)rt->m_光追管线);

		光追ID += E_光追阶段ID::e_光追阶段ID_材质回调;
		loc += E_光追阶段ID::e_光追阶段ID_材质回调;


		uint32 num = pipeline->m_着色->m_光追着色组创建信息.size();
		for (uint32 i = 0; i < num; ++i) {
			const VkRayTracingShaderGroupCreateInfoKHR& 光追着色组 = pipeline->m_着色->m_光追着色组创建信息[i];
			if (光追着色组.generalShader == 光追ID) {
				//loc = i;
				break;
			}
		}

		if (pipeline->m_着色->m_光追着色器.size() <= loc) {
			for (uint32 i = pipeline->m_着色->m_光追着色器.size(); i <= loc; ++i) {
				pipeline->m_着色->m_着色阶段创建信息.push_back({});

				pipeline->m_着色->m_光追着色器.push_back({ E_着色阶段::e_光追回调 });
				pipeline->m_着色->m_光追着色组创建信息.push_back({});
			}
		}
		else {
			f_vkShader_销毁着色器(pipeline->m_着色, loc, true);
		}

		//if (pipeline->m_着色->m_光追着色器.size() <= loc) {
		//	std::cout << "fuck" << std::endl;
		//}

		auto code = f_file_读SPV文件(path);
		f_vkShader_创建着色器(pipeline->m_着色, code, E_着色阶段::e_光追回调, loc, true, 光追ID);

		rt->m_重新编译线管 = true;
	}
}

void f_bd_删除光追材质着色器(S_光追后端* rt, int32 loc) {
	auto* pipeline = ((S_Vk光追线管*)rt->m_光追管线);
	loc += E_光追阶段ID::e_光追阶段ID_材质回调;

	pipeline->f_销毁着色器(loc);
	rt->m_重新编译线管 = true;
}


void f_df_构建顶层光追加速(S_渲染层& 渲染层, uint32 num, bool 更新) {
	auto* 绘图环境 = f_vk_get绘图环境(渲染层.m_Ctx);
	auto* 绘图框架 = f_vk_get绘图框架(渲染层.m_Ctx);
	//S_VK绘图设备* 绘图配置 = (S_VK绘图设备*)f_df_get绘图环境(渲染层.m_Ctx);

	VkAccelerationStructureGeometryKHR m_顶层几何实例加速结构{};
	VkAccelerationStructureBuildGeometryInfoKHR m_加速结构绑定几何实例信息{};

	uint32_t primitive_count = num;
	uint32 分配大小 = 0;
	static S_GPU内存块 临时缓存;
	
	
	
	m_顶层几何实例加速结构.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_GEOMETRY_KHR;
	m_顶层几何实例加速结构.geometryType = VK_GEOMETRY_TYPE_INSTANCES_KHR;
	m_顶层几何实例加速结构.flags = VK_GEOMETRY_OPAQUE_BIT_KHR;
	m_顶层几何实例加速结构.geometry.instances.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_GEOMETRY_INSTANCES_DATA_KHR;
	m_顶层几何实例加速结构.geometry.instances.arrayOfPointers = VK_FALSE;
	m_顶层几何实例加速结构.geometry.instances.data.deviceAddress = f_buf_getAddress2(渲染层.m_光追实例变换);


	
	for (uint32 i = 0; i < 1; ++i) {
		m_加速结构绑定几何实例信息.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_BUILD_GEOMETRY_INFO_KHR;
		m_加速结构绑定几何实例信息.type = VK_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL_KHR;
		m_加速结构绑定几何实例信息.flags = VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_TRACE_BIT_KHR;
		m_加速结构绑定几何实例信息.geometryCount = 1;
		m_加速结构绑定几何实例信息.pGeometries = &m_顶层几何实例加速结构;
		

		VkAccelerationStructureBuildSizesInfoKHR m_加速结构绑定实例大小信息 = { VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_BUILD_SIZES_INFO_KHR };
		绘图框架->vkGetAccelerationStructureBuildSizes(
			绘图环境->m_设备,
			VK_ACCELERATION_STRUCTURE_BUILD_TYPE_DEVICE_KHR,
			&m_加速结构绑定几何实例信息,
			&primitive_count,
			&m_加速结构绑定实例大小信息);

		分配大小 = (m_加速结构绑定实例大小信息.accelerationStructureSize / 256) + 1;
		分配大小 *= 256;

		if (临时缓存.m_Buf) {
			f_bm_resize(临时缓存, 分配大小);
		}
		else {
			临时缓存 = f_bm_alloc(f_buf_getGlobal().g_顶层加速结构构建缓存, 分配大小);
		}
	}
	f_bm_resize(渲染层.m_光追顶层加速结构, 分配大小);

	
	
	m_加速结构绑定几何实例信息.type = VK_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL_KHR;
	m_加速结构绑定几何实例信息.flags = VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_TRACE_BIT_KHR;
	m_加速结构绑定几何实例信息.mode = VK_BUILD_ACCELERATION_STRUCTURE_MODE_BUILD_KHR;
	//m_加速结构绑定几何实例信息.srcAccelerationStructure;
	m_加速结构绑定几何实例信息.dstAccelerationStructure = (VkAccelerationStructureKHR)f_buf_get加速结构(渲染层.m_光追顶层加速结构);
	m_加速结构绑定几何实例信息.scratchData.deviceAddress = f_buf_getAddress2(临时缓存);
	
	
	VkAccelerationStructureBuildRangeInfoKHR 实例区间;
	实例区间.primitiveOffset = 0;
	实例区间.firstVertex = 0;
	实例区间.transformOffset = 0;
	实例区间.primitiveCount = num;
	const VkAccelerationStructureBuildRangeInfoKHR* 绑定偏移信息 = &实例区间;

	f_Draw_Begin(&渲染层.m_Ctx, S_光追后端::g_加速结构创建命令缓存, S_光追后端::g_渲染Fence);
	绘图框架->vkCmdBuildAccelerationStructures((VkCommandBuffer)S_光追后端::g_加速结构创建命令缓存, 1, &m_加速结构绑定几何实例信息, &绑定偏移信息);
	f_Draw_end(&渲染层.m_Ctx, S_光追后端::g_加速结构创建命令缓存, S_光追后端::g_渲染Fence, 3);

}














//void C_Vk光追后端::f_添加顶层加速结构(S_GPU内存块索引& TLAS, S_GPU内存块索引& 实例加速结构) {
//	
//	uint32 实例数量 = 实例加速结构.m_Mem.m_数量;
//	//for (auto& e : m_实例加速结构) {
//	//	实例数量 += e->m_Mem.m_数量;
//	//}
//	if (!TLAS.m_Buf) {
//		static uint32 顶层加速结构_交叉分配 = 0;
//		if (顶层加速结构_交叉分配 & 1) {
//			TLAS = f_buf_alloc(S_Mesh::g_TLAS_0, 0);
//		}
//		else {
//			TLAS = f_buf_alloc(S_Mesh::g_TLAS_1, 0);
//			//TLAS = f_buf_alloc(S_Mesh::g_TLAS_0, 0);
//		}
//		++顶层加速结构_交叉分配;
//	}
//	m_顶层加速结构内存块.push_back(&TLAS);
//}

//void C_Vk光追后端::f_构建顶层加速结构() {
//	auto* 绘图配置 = (S_绘图设备配置*)m_绘图环境;
//
//	
//	m_实例区间.clear();
//	std::vector<VkAccelerationStructureBuildGeometryInfoKHR> 加速结构绑定几何实例信息;
//	uint32 num = m_顶层加速结构内存块.size();
//
//
//	std::vector<VkAccelerationStructureGeometryKHR> 几何加速结构;
//	for (uint32 i = 0; i < num; ++i) {
//		VkDeviceOrHostAddressConstKHR 实例数据{};
//		实例数据.deviceAddress = f_buf_getAddress(*m_实例加速结构[i]);
//
//		VkAccelerationStructureGeometryKHR asg{ VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_GEOMETRY_KHR }; {
//			asg.geometryType = VK_GEOMETRY_TYPE_INSTANCES_KHR;
//			asg.flags = VK_GEOMETRY_OPAQUE_BIT_KHR;
//			asg.geometry.instances.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_GEOMETRY_INSTANCES_DATA_KHR;
//			asg.geometry.instances.arrayOfPointers = VK_FALSE;
//			asg.geometry.instances.data = 实例数据;
//		}
//		几何加速结构.push_back(asg);
//
//
//		VkAccelerationStructureBuildRangeInfoKHR 实例区间{};
//		实例区间.primitiveCount = m_实例加速结构[i]->m_Mem.m_数量;
//		实例区间.primitiveOffset = 0;
//		实例区间.firstVertex = 0;
//		实例区间.transformOffset = 0;
//		//实例区间.primitiveCount = 1;
//		m_实例区间.push_back(&实例区间);
//	}
//
//	std::vector<VkAccelerationStructureBuildGeometryInfoKHR> 加速几何绑定;
//	for (uint32 i = 0; i < num; ++i) {
//		VkAccelerationStructureBuildGeometryInfoKHR info;
//		info = { VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_BUILD_GEOMETRY_INFO_KHR };
//		info.type = VK_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL_KHR;
//		info.flags = VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_TRACE_BIT_KHR;
//		info.geometryCount = 1;
//		info.pGeometries = &几何加速结构[i];
//		加速几何绑定.push_back(info);
//
//	}
//	
//	for (uint32 i = 0; i < num; ++i) {
//		//uint32 实例数量 = 几何加速结构.size();
//		uint32 实例数量 = m_实例加速结构[i]->m_Mem.m_数量;
//
//		
//		VkAccelerationStructureBuildGeometryInfoKHR info;
//		info = { VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_BUILD_GEOMETRY_INFO_KHR };
//		info.type = VK_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL_KHR;
//		info.flags = VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_TRACE_BIT_KHR;
//		info.geometryCount = 1;
//		info.pGeometries = &几何加速结构[i];
//
//
//		S_板载缓存* 暂存缓冲区 = i & 1 ? m_顶层暂存缓冲区_1 : m_顶层暂存缓冲区;
//		//S_板载缓存* 暂存缓冲区 = m_顶层暂存缓冲区;
//
//		uint32_t primitive_count = 几何加速结构.size();
//		//uint32_t primitive_count = 1;
//		m_加速结构绑定实例大小信息 = { VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_BUILD_SIZES_INFO_KHR };
//		绘图配置->vkGetAccelerationStructureBuildSizes(
//			绘图配置->m_设备,
//			VK_ACCELERATION_STRUCTURE_BUILD_TYPE_DEVICE_KHR,
//			加速几何绑定.data(),
//			&primitive_count,
//			&m_加速结构绑定实例大小信息);
//
//		f_gbuf_resize(*m_顶层加速结构内存块[i], m_加速结构绑定实例大小信息.accelerationStructureSize);
//		f_buf_resize(暂存缓冲区, m_加速结构绑定实例大小信息.buildScratchSize);
//
//		info.type = VK_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL_KHR;
//		info.flags = VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_TRACE_BIT_KHR;
//		info.mode = VK_BUILD_ACCELERATION_STRUCTURE_MODE_BUILD_KHR;
//		//info.dstAccelerationStructure = (VkAccelerationStructureKHR)f_buf_get加速结构(*m_顶层加速结构内存块[i]);
//		//info.scratchData.deviceAddress = f_buf_getAddress(static_cast<S_VkArray缓存*>(暂存缓冲区));
//		加速结构绑定几何实例信息.push_back(info);
//		
//		
//		//f_Draw_Begin(m_Ctx, m_加速结构创建命令缓存, m_渲染Fence);
//		//绘图配置->vkCmdBuildAccelerationStructures((VkCommandBuffer)m_加速结构创建命令缓存, 1, &info, m_实例区间.data());
//		//f_Draw_end(m_Ctx, m_加速结构创建命令缓存, m_渲染Fence, S_光追后端::g_光追列队);
//		//return;
//	}
//
//	
//	for (uint32 i = 0; i < num; ++i) {
//		加速结构绑定几何实例信息[i].dstAccelerationStructure = (VkAccelerationStructureKHR)f_buf_get加速结构(*m_顶层加速结构内存块[i]);
//		//加速结构绑定几何实例信息[i].srcAccelerationStructure = (VkAccelerationStructureKHR)f_buf_get加速结构(*m_顶层加速结构内存块[i]);
//	
//		S_板载缓存* 暂存缓冲区 = i & 1 ? m_顶层暂存缓冲区_1 : m_顶层暂存缓冲区;
//		加速结构绑定几何实例信息[i].scratchData.deviceAddress = f_buf_getAddress(static_cast<S_VkArray缓存*>(暂存缓冲区));
//		
//	}
//	
//	for (uint32 i = 0; i < num; ++i) {
//		f_Draw_Begin(m_Ctx, m_加速结构创建命令缓存, m_渲染Fence);
//		绘图配置->vkCmdBuildAccelerationStructures((VkCommandBuffer)m_加速结构创建命令缓存, 1, &加速结构绑定几何实例信息[i], &m_实例区间[i]);
//		f_Draw_end(m_Ctx, m_加速结构创建命令缓存, m_渲染Fence, S_光追后端::g_光追列队);
//	}
//}







